﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WT.Sockert;
using WT.Sockert.Interfaces;

namespace WT.ParkingShare
{
    public partial class Service : Form
    {
        

        public Service()
        {
            InitializeComponent();
        }

        public Dictionary<string, ISocketConnection> conList = new Dictionary<string, ISocketConnection>();
        public Dictionary<string, List<string>> msgClinetToService = new Dictionary<string, List<string>>();
        public Dictionary<string, List<string>> msgServiceToClinet = new Dictionary<string, List<string>>();


        private int i = 0;
        private void btnService_Click(object sender, EventArgs e)
        {
            SocketServer server = new SocketServer("0.0.0.0");
            server.Start(socket => {
                socket.OnOpen = () => {
                    var t = socket;
                    conList.Add((i++).ToString()+":"+socket.Name, socket);

                    //添加连接客户端
                    ControlClient(socket);
                };
                socket.OnClose = () => {
                    //关闭链接客户端
                    conList.Remove(socket.Name);
                };
                socket.OnMessage = msg => {
                    
                    //msgClinetToService[socket.Name].Add(msg);
                };
            });
        }
        delegate void delegateAddControl(UserControl control);

        public void AddUserControl(UserControl control) {

            if (panelClient.InvokeRequired)
            {

                delegateAddControl de = new delegateAddControl(AddUserControl);
                panelClient.Invoke(de,control);
            } else {
                panelClient.Controls.Add(control);
            }


        }

        private void ControlClient(ISocketConnection socket) {

            ClientControl temp = new ClientControl(socket);

            AddUserControl(temp);
            


        }
        
        private void btnSend_Click(object sender, EventArgs e)
        {
            
            
        }
        private Socket _clientSocket = null;
        private void btnCreateClient_Click(object sender, EventArgs e)
        {
            int port = 80;
            string host = "123.207.244.249";//服务器端ip地址

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
             
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _clientSocket.Connect(ipe);

              // 
            _clientSocket.Send(Encoding.ASCII.GetBytes("id:000000000001"));
            

            //send message
            ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
            Thread thread = new Thread(pts);
            thread.IsBackground = true;
            //启动线程
            thread.Start(_clientSocket);


        }

        private void ServerRecMsg(object socketClientPara)
        {
            Socket socketServer = socketClientPara as Socket;
            
            while (true)
            {
                
                byte[] buffer = new byte[1024];
                try
                {
                    //获取接收的数据,并存入内存缓冲区  返回一个字节数组的长度
                    socketServer.Receive(buffer);
                    var  strSRecMsg = Encoding.UTF8.GetString(buffer, 0, buffer.Length);//真实有用的文本信息要比接收到的少1(标识符)
                    textLog.writeLog(strSRecMsg);
                }
                catch (Exception ex)
                {
                    
                    break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(() =>
            {
                string sendStr = textPort.Text;// this.texValue.Text;
                byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
              //  _clientSocket.Send(sendBytes);

                //receive message
                //string recStr = "";
                //byte[] recBytes = new byte[4096];
                //int bytes = _clientSocket.Receive(recBytes, recBytes.Length, 0);
                //recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
            });
            thread.Start();
            
        }

        private void btnClientClose_Click(object sender, EventArgs e)
        {
            _clientSocket.Close();
        }
    }
}
